package com.heima.search.service.Impl;

import com.heima.common.dto.ResponseResult;
import com.heima.common.entity.User;
import com.heima.common.util.AppThreadLocal;
import com.heima.search.dto.ApArticleSearchDto;
import com.heima.search.entity.ApUserSearch;
import com.heima.search.service.IArticleLoadService;
import com.heima.search.service.IArticleSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ArticleLoadServiceImpl implements IArticleLoadService {
    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 搜索记录分页查询
     * @param dto
     * @return
     */
    @Override
    public ResponseResult load(ApArticleSearchDto dto) {
        Query query = new Query();
        User user = AppThreadLocal.get();
        if (user != null){
            query.addCriteria(Criteria.where("userId").is(user.getUserId()));
        }else {
            query.addCriteria(Criteria.where("equipmentId").is(dto.getEquipmentId()));
        }
//        排序
        Pageable page = PageRequest.of(0,dto.getSize(), Sort.Direction.DESC,"createdTime");
        query.with(page);
        //        查询数据
        List<ApUserSearch> apUserSearches = mongoTemplate.find(query, ApUserSearch.class);


        return ResponseResult.okResult(apUserSearches);
    }

    /**
     * 异步调用（搜索分页查询异步调用添加查询记录）
     * @param userId
     * @param equipmentId
     * @param searchWords
     */
    @Override
    @Async
    public void saveSearchRecord(Integer userId, String equipmentId, String searchWords) {
        ApUserSearch apUserSearch = new ApUserSearch();
        if (userId != null){
            apUserSearch.setUserId(userId);
        }else {
            apUserSearch.setEquipmentId(equipmentId);
        }
        apUserSearch.setCreatedTime(new Date());
        apUserSearch.setKeyword(searchWords);

        Query query = new Query();
        if (userId != null){
            query.addCriteria(Criteria.where("userId").is(userId));
        }else {
            query.addCriteria(Criteria.where("equipmentId").is(equipmentId));
        }
        query.addCriteria(Criteria.where("keyword").is(searchWords));
        long count = mongoTemplate.count(query, ApUserSearch.class);
        if (count == 0) {
            mongoTemplate.save(apUserSearch);
        }
    }

    /**
     * 搜索记录删除
     * @param dto
     * @return
     */
    @Override
    public ResponseResult del(ApArticleSearchDto dto) {
        List<ApUserSearch> hisList = dto.getHisList();
        List<String> list = new ArrayList<>();
        for (ApUserSearch apUserSearch : hisList) {
            list.add(apUserSearch.getId());
        }
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(list));
        mongoTemplate.remove(query,ApUserSearch.class);

        return ResponseResult.okResult();
    }


}
